home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ZMATH.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  5KB  |  220 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zmath.c */
  20. /* Mathematical operators for Ghostscript */
  21. #include "math_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "store.h"
  26.  
  27. /* Current state of random number generator. */
  28. /* We have to implement this ourselves because */
  29. /* the Unix rand doesn't provide anything equivalent to rrand. */
  30. private long rand_state;
  31.  
  32. /* Initialize the random number generator. */
  33. private void
  34. zmath_init(void)
  35. {    rand_state = 1;
  36. }
  37.  
  38. /****** NOTE: none of these operators currently ******/
  39. /****** check for floating over- or underflow.    ******/
  40.  
  41. /* <num> sqrt <real> */
  42. int
  43. zsqrt(register os_ptr op)
  44. {    float num;
  45.     int code = num_params(op, 1, &num);
  46.     if ( code < 0 )
  47.         return code;
  48.     if ( num < 0.0 )
  49.         return_error(e_rangecheck);
  50.     make_real(op, sqrt(num));
  51.     return 0;
  52. }
  53.  
  54. /* <num> arccos <real> */
  55. int
  56. zarccos(register os_ptr op)
  57. {    float num, result;
  58.     int code = num_params(op, 1, &num);
  59.     if ( code < 0 ) return code;
  60.     result = acos(num) * radians_to_degrees;
  61.     make_real(op, result);
  62.     return 0;
  63. }
  64.  
  65. /* <num> arcsin <real> */
  66. int
  67. zarcsin(register os_ptr op)
  68. {    float num, result;
  69.     int code = num_params(op, 1, &num);
  70.     if ( code < 0 ) return code;
  71.     result = asin(num) * radians_to_degrees;
  72.     make_real(op, result);
  73.     return 0;
  74. }
  75.  
  76. /* <num> <denom> atan <real> */
  77. int
  78. zatan(register os_ptr op)
  79. {    float args[2];
  80.     float result;
  81.     int code = num_params(op, 2, args);
  82.     if ( code < 0 ) return code;
  83.     if ( args[0] == 0 )        /* on X-axis, special case */
  84.        {    if ( args[1] == 0 )
  85.             return_error(e_undefinedresult);
  86.         result = (args[1] < 0 ? 180 : 0);
  87.        }
  88.     else
  89.        {    result = atan2(args[0], args[1]) * radians_to_degrees;
  90.         if ( result < 0 ) result += 360;
  91.        }
  92.     make_real(op - 1, result);
  93.     pop(1);
  94.     return 0;
  95. }
  96.  
  97. /* <num> cos <real> */
  98. int
  99. zcos(register os_ptr op)
  100. {    float angle;
  101.     int code = num_params(op, 1, &angle);
  102.     if ( code < 0 ) return code;
  103.     make_real(op, cos(angle * degrees_to_radians));
  104.     return 0;
  105. }
  106.  
  107. /* <num> sin <real> */
  108. int
  109. zsin(register os_ptr op)
  110. {    float angle;
  111.     int code = num_params(op, 1, &angle);
  112.     if ( code < 0 ) return code;
  113.     make_real(op, sin(angle * degrees_to_radians));
  114.     return 0;
  115. }
  116.  
  117. /* <base> <exponent> exp <real> */
  118. int
  119. zexp(register os_ptr op)
  120. {    float args[2];
  121.     float result;
  122.     double ipart;
  123.     int code = num_params(op, 2, args);
  124.     if ( code < 0 ) return code;
  125.     if ( args[0] == 0.0 && args[1] == 0.0 )
  126.         return_error(e_undefinedresult);
  127.     if ( args[0] < 0.0 && modf(args[1], &ipart) != 0.0 )
  128.         return_error(e_undefinedresult);
  129.     result = pow(args[0], args[1]);
  130.     make_real(op - 1, result);
  131.     pop(1);
  132.     return 0;
  133. }
  134.  
  135. /* <posnum> ln <real> */
  136. int
  137. zln(register os_ptr op)
  138. {    float num;
  139.     int code = num_params(op, 1, &num);
  140.     if ( code < 0 )
  141.         return code;
  142.     if ( num <= 0.0 )
  143.         return_error(e_rangecheck);
  144.     make_real(op, log(num));
  145.     return 0;
  146. }
  147.  
  148. /* <posnum> log <real> */
  149. int
  150. zlog(register os_ptr op)
  151. {    float num;
  152.     int code = num_params(op, 1, &num);
  153.     if ( code < 0 )
  154.         return code;
  155.     if ( num <= 0.0 )
  156.         return_error(e_rangecheck);
  157.     make_real(op, log10(num));
  158.     return 0;
  159. }
  160.  
  161. /* - rand <int> */
  162. int
  163. zrand(register os_ptr op)
  164. {    /*
  165.      * We use an algorithm from CACM 31 no. 10, pp. 1192-1201,
  166.      * October 1988.  According to a posting by Ed Taft on
  167.      * comp.lang.postscript, Level 2 (Adobe) PostScript interpreters use
  168.      * this algorithm too:
  169.      *    x[n+1] = (16807 * x[n]) mod (2^31 - 1)
  170.      */
  171. #define A 16807
  172. #define M 0x7fffffff
  173. #define Q 127773            /* M / A */
  174. #define R 2836                /* M % A */
  175.     rand_state = A * (rand_state % Q) - R * (rand_state / Q);
  176.     while ( rand_state <= 0 ) rand_state += M;
  177. #undef A
  178. #undef M
  179. #undef Q
  180. #undef R
  181.     push(1);
  182.     make_int(op, rand_state);
  183.     return 0;
  184. }
  185.  
  186. /* <int> srand - */
  187. int
  188. zsrand(register os_ptr op)
  189. {    check_type(*op, t_integer);
  190.     rand_state = op->value.intval;
  191.     pop(1);
  192.     return 0;
  193. }
  194.  
  195. /* - rrand <int> */
  196. int
  197. zrrand(register os_ptr op)
  198. {    push(1);
  199.     make_int(op, rand_state);
  200.     return 0;
  201. }
  202.  
  203. /* ------ Initialization procedure ------ */
  204.  
  205. op_def zmath_op_defs[] = {
  206.     {"1arccos", zarccos},        /* extension */
  207.     {"1arcsin", zarcsin},        /* extension */
  208.     {"2atan", zatan},
  209.     {"1cos", zcos},
  210.     {"2exp", zexp},
  211.     {"1ln", zln},
  212.     {"1log", zlog},
  213.     {"0rand", zrand},
  214.     {"0rrand", zrrand},
  215.     {"1sin", zsin},
  216.     {"1sqrt", zsqrt},
  217.     {"1srand", zsrand},
  218.     op_def_end(zmath_init)
  219. };
  220.